package com.bolt.admin.web.view.jsp;

import com.bolt.admin.module.sys.entity.TableConfig;
import com.bolt.admin.module.sys.entity.TableHeadConfig;
import com.bolt.admin.module.sys.service.TableConfigService;
import com.bolt.common.utils.SpringContextUtil;
import com.bolt.common.utils.StrUtil;
import com.bolt.core.view.components.ComponentsBase;
import com.bolt.core.view.components.DataGrid;
import com.bolt.core.view.components.TableHead;
import com.bolt.core.view.config.UIProperties;
import com.bolt.core.view.tag.TemplateBodyTagSupport;
import com.bolt.core.view.templates.FreemarkerTemplateEngine;

import javax.servlet.jsp.JspException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


/**
 * @author 米超
 * 列表标签
 */
public class DataGridTag extends TemplateBodyTagSupport {

	private static final long serialVersionUID = 1L;

	private String domId;

    @Override
    public ComponentsBase getUI() {
        DataGrid dataGrid =	createTableByContext();
        if(null == dataGrid){
            return createTableByDb();
        }else{
            return dataGrid;
        }
    }

    @Override
	public int doStartTag() throws JspException {
		component = getUI();
        SpringContextUtil.inject(component);
		evaluateExpressions();
		return EVAL_BODY_INCLUDE;
	}

	@Override
	public int doEndTag() throws JspException {
		component.end(getRequest(),getResponse(),pageContext.getOut());
		return EVAL_PAGE;
	}
    
	private DataGrid createTableByContext(){
		Object dbTable =  getRequest().getAttribute(getId());
		if(null != dbTable){
			return (DataGrid)dbTable;
		}else{
			return null;
		}
	}
	
	private DataGrid createTableByDb(){
        TableConfigService tableconfigService = SpringContextUtil.getBean(TableConfigService.class);
        FreemarkerTemplateEngine engine = SpringContextUtil.getBean(FreemarkerTemplateEngine.class);
        UIProperties uiProperties = SpringContextUtil.getBean(UIProperties.class);
        TableConfig tableconfig = tableconfigService.findTableConfig("",getId());
		DataGrid table = new DataGrid();
		table.setId(StrUtil.isNotBlank(domId)?domId:getId());
		table.setTableId(StrUtil.isNotBlank(domId)?domId:getId());
		table.setTableTitle(tableconfig.getTableTitle());
		String s = getRequest().getQueryString();
		if (StrUtil.isNotBlank(s)&& StrUtil.isNotBlank(tableconfig.getTableUrl())) {
			table.setUrl(tableconfig.getTableUrl()+"?"+s);
		}else{
			table.setUrl(tableconfig.getTableUrl());
		}
        table.setToolbar(tableconfig.getToolbar());
		table.setPagination(tableconfig.isPagination());
		table.setAutoCreate(tableconfig.isAutoCreate());
        table.setTheme(uiProperties.getTheme());
        table.setTemplateDir(uiProperties.getDefaultPath());
        table.setTemplateEngine(engine);
		table.setCheckBox(tableconfig.isCheckbox());
		List<TableHead> heads = new ArrayList<>();
		List<TableHeadConfig>  tableHeads = tableconfig.getTableHeads();
		Collections.sort(tableHeads);
		for(TableHeadConfig tableHead :tableHeads){
			TableHead head = new TableHead();
			head.setField(tableHead.getField());
			head.setTitle(tableHead.getTitle());
			head.setListAlign(tableHead.getAlign());
			head.setFormat(StrUtil.isEmpty(tableHead.getRender())?null:tableHead.getRender());
			head.setHidden(tableHead.getHidden());
			head.setWidth(tableHead.getWidth());
			head.setEditor(tableHead.getEditType());
			head.setOrder(tableHead.getSortable());
			heads.add(head);
			
		}
		table.setHeads(heads);
		return table;
	}

	public String getDomId() {
		return domId;
	}

	public void setDomId(String domId) {
		this.domId = domId;
	}
}
